﻿// (c) Andreas Kreisl
// You are not permitted to use this in any way!

var mod_cpumonitor_debug = false | global_debug;

var g;
var mod_discmonitor_timer;
var mod_discmonitor_dock;
var r = new Array();

mod_discmonitor_PrefLanguage();

var doc = XMLDOM.parse( filesystem.readFile( "dock.xml" ) );
var doc_text1 = doc.getElementById( "text1" );
var doc_text2 = doc.getElementById( "text2" );


















var net_USED = preferences.usedID.value;
var net_NAME = new Array();

if(system.platform=="windows") {
	include("os_windows.js");
} else if(system.platform=="macintosh") {
	include("os_macintosh.js");
} else {
	alert('Unsupported OS detected!');
	closeWidget();
}

GenerateDeviceList();
if(net_USED == -1) {
	alert('No Hard Disk detected!\nOr WMI is not installed correct.');
	closeWidget();
}

main_menu[++main_menu_i] = new MenuItem();
main_menu[main_menu_i].title = "-";

for(j in net_NAME) {
	main_menu[++main_menu_i] = new MenuItem();
	main_menu[main_menu_i].title = net_NAME[j];
	main_menu[main_menu_i].net_USED = j;
	main_menu[main_menu_i].onSelect = mod_discmonitor_SetActiveDevice;
	if(net_USED == j) {
		main_menu[main_menu_i].enabled = false;
		main_menu[main_menu_i].checked = true;
	} else {
		main_menu[main_menu_i].enabled = true;
		main_menu[main_menu_i].checked = false;
	}
}

var net_BW = GetDeviceBW();








function mod_discmonitor_SetActiveDevice() {
	for(i=0;i<main_menu.length;i++)
	if(main_menu[i].title ==net_NAME[net_USED]) {
		main_menu[i].enabled = true;
		main_menu[i].checked = false;
	}

	preferences.usedID.value = this.net_USED;
	savePreferences();
	net_USED = this.net_USED;
	net_BW = GetDeviceBW();
	g.db.exec("DELETE FROM graph");

	for(i=0;i<main_menu.length;i++)
	if(main_menu[i].title ==net_NAME[net_USED]) {
		main_menu[i].enabled = false;
		main_menu[i].checked = true;
	}

	my_net_in_last = 0;
	my_net_out_last = 0;


	mod_discmonitor_getvalues();
	//g.redraw();
	return true;
}









function mod_discmonitor_init() {
	g = new class_graph();

	g.add(50,"#00FF00",1,-1);
	g.add(50,"#FF0000",-1,-1);

	mod_discmonitor_timer();
	mod_discmonitor_PrefChange();
	main.visible = true;


	main_menu[++main_menu_i] = new MenuItem();
	main_menu[main_menu_i].title = "-";
	main_menu[++main_menu_i] = new MenuItem();
	main_menu[main_menu_i].title = "Screenshot!";
	main_menu[main_menu_i].onSelect = function ( ) {
		g.saveImageToFile();
		return true;
	}

	return true;
}

function mod_discmonitor_PrefLanguage() {
	preferences.ScaleY.title            = widget.getLocalizedString("pref_ScaleY");
	preferences.ScaleX.title            = widget.getLocalizedString("pref_ScaleX");
	preferences.GraphDir.title          = widget.getLocalizedString("pref_GraphDir");
	preferences.GraphDir.option[0]      = widget.getLocalizedString("pref_GraphDir_0");
	preferences.GraphDir.option[1]      = widget.getLocalizedString("pref_GraphDir_1");
	preferences.GraphUpdateSpeed.title   = widget.getLocalizedString("pref_GraphUpdateSpeed");
	preferences.GraphUpdateSpeed.tickLabel[1] = widget.getLocalizedString("pref_DockUpdateSpeedSec");
	preferences.DockUpdateSpeed.title   = widget.getLocalizedString("pref_DockUpdateSpeed");
	preferences.DockUpdateSpeed.tickLabel[1] = widget.getLocalizedString("pref_DockUpdateSpeedSec");
	preferences.ColorBorder.title       = widget.getLocalizedString("pref_color_Border");
	preferences.ColorBackground.title   = widget.getLocalizedString("pref_color_Background");
	preferences.ColorText.title         = widget.getLocalizedString("pref_color_Text");
	preferences.ColorIN.title           = widget.getLocalizedString("pref_color_IN");
	preferences.ColorOUT.title          = widget.getLocalizedString("pref_color_OUT");
	preferences.OpacityBorder.title     = widget.getLocalizedString("pref_opazity_Border");
	preferences.OpacityBackground.title = widget.getLocalizedString("pref_opazity_Background");
	preferences.TextSize.title          = widget.getLocalizedString("pref_text_size");
	preferenceGroups.pref_style.title   = widget.getLocalizedString("prefgrp_style");
	preferenceGroups.pref_color.title   = widget.getLocalizedString("prefgrp_color");
	preferences.txshow.title            = widget.getLocalizedString("pref_txshow");
	preferences.txtext.title            = widget.getLocalizedString("pref_txtext");

	return true
}

function mod_discmonitor_timer() {
	mod_discmonitor_timer              = new Timer();
	mod_discmonitor_timer.interval     = 1.0;
	mod_discmonitor_timer.ticking      = true;
	mod_discmonitor_timer.onTimerFired = mod_discmonitor_getvalues;

	mod_discmonitor_dock               = new Timer();
	mod_discmonitor_dock.interval      = 5.0;
	mod_discmonitor_dock.ticking       = widget.dockOpen;
	mod_discmonitor_dock.onTimerFired  = mod_discmonitor_updatedock;

	return true;
}

function mod_discmonitor_getvalues(){
	r[0] = GetDeviceIN();
	r[1] = GetDeviceOUT();

	if(r[0]<0)r[0]=0;
	if(r[1]<0)r[1]=0;

	g.update(r[0],r[1]);
	if(preferences.txshow.value==1) {
		if(preferences.txtext.value==0) {
			g.Graph_Text[0].data = Math.round(r[0]/1024);
			g.Graph_Text[1].data = Math.round(r[1]/1024);
		} else {
			g.Graph_Text[0].data = Math.round(r[0]/1024) + " KBps";
			g.Graph_Text[1].data = Math.round(r[1]/1024) + " KBps";
		}
	}
	return true;
}

function mod_discmonitor_updatedock(){
	doc_text1.setAttribute( "data",  Math.round(r[0]/1024));
	doc_text2.setAttribute( "data",  Math.round(r[1]/1024));
	widget.setDockItem( doc , "fade" );
	return true;
}

function mod_discmonitor_PrefChange() {
	g.height = parseInt(preferences.ScaleY.value);
	g.width = parseInt(preferences.ScaleX.value);
	g.graphdir = parseInt(preferences.GraphDir.value)

	g.Background_Color = preferences.ColorBackground.value;
	g.Border_Color = preferences.ColorBorder.value;

	g.Background_Canvas.opacity = parseInt(preferences.OpacityBackground.value)*255/100;
	g.Border_Canvas.opacity = parseInt(preferences.OpacityBorder.value)*255/100;

	g.Graph_Color[0] = preferences.ColorIN.value;
	g.Graph_Color[1] = preferences.ColorOUT.value;

	g.Graph_Text[0].color = preferences.ColorText.value;
	g.Graph_Text[1].color = preferences.ColorText.value;

	for(i=0;i<g.Graph_Text.length;i++) {
		g.Graph_Text[i].style.KonShadow = "1px 1px " + class_graph_ColorModify(g.Graph_Color[i],64);
		g.Graph_Text[i].size = parseInt(preferences.TextSize.value);

		g.Graph_Text[i].visible = preferences.txshow.value;
	}
	g.textsize = parseInt(preferences.TextSize.value);

	mod_discmonitor_dock.interval = parseInt(preferences.DockUpdateSpeed.value);
	mod_discmonitor_timer.interval = parseInt(preferences.GraphUpdateSpeed.value);

	g.draw();
	g.redraw();
	return true;
}
